package my.app.timer;

import java.util.Locale;

import com.google.android.youtube.player.YouTubePlayerSupportFragment;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Chronometer;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener 
{

	private boolean resetFlag;
	private boolean pauseFlag;
	private Chronometer chro;
	private long pausedAt;
	private PowerManager.WakeLock wakelck;
	SectionsPagerAdapter mSectionsPagerAdapter;
	ViewPager mViewPager;
	NotificationManager notificationManager;
	MenuItem exititem;
	MenuItem channel;
	
	
	@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Set up the action bar.
		final ActionBar actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
       
        wakelck = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "DoNotDimScreen");
        wakelck.acquire();
		
        Intent intent = new Intent();
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,Intent.FLAG_ACTIVITY_NEW_TASK);
        BitmapFactory bm = new BitmapFactory();
        Bitmap bmp = bm.decodeResource(getResources(), R.drawable.stop11);

        // build notification
        // the addAction re-use the same intent to keep the example short
        Notification n  = new Notification.Builder(this)
                .setContentTitle("Bla bla")
                .setContentText("Subject")
                .setSmallIcon(R.drawable.ic_launcher)
                .setLargeIcon(bmp)
                .setContentIntent(pIntent)
                .setAutoCancel(false)
                .addAction(R.drawable.stop1, "And more", pIntent).build();
            
          
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(0, n); 
        
		mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

		mViewPager = (ViewPager) findViewById(R.id.pager);
		mViewPager.setAdapter(mSectionsPagerAdapter);
		
		chro = (Chronometer) findViewById( R.id.chrono );
		resetFlag = true;
    	pauseFlag = false;
		
		mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
					@Override
					public void onPageSelected(int position) {
						actionBar.setSelectedNavigationItem(position);
					}
				});

		// For each of the sections in the app, add a tab to the action bar.
		for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
			actionBar.addTab(actionBar.newTab()
					.setText(mSectionsPagerAdapter.getPageTitle(i))
					.setTabListener(this));
		}
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		wakelck.release();
		notificationManager.cancelAll();
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		channel = menu.findItem(R.id.chnl);
		exititem = menu.findItem(R.id.exititem);
		return true;
	}
	
	public void exit(MenuItem v)
    {
		notificationManager.cancelAll();
		System.exit(0);  	
    }
	
	
	@Override
	public void onTabSelected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
		
		mViewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
	}

	@Override
	public void onTabReselected(ActionBar.Tab tab,
			FragmentTransaction fragmentTransaction) {
	}

	public class SectionsPagerAdapter extends FragmentPagerAdapter 
	{
		public SectionsPagerAdapter(FragmentManager fm) 
		{
			super(fm);
		}
		
		@Override
		public Fragment getItem(int position) {
		
			Fragment fragment1 = new PlayerFragment();
			//Fragment fragment2 = new DummySectionFragment();

			Fragment fragment2 = new MyFragment();
			//Fragment fragment3 = new DummySectionFragment();
			
			switch (position) {
			case 0:
				return fragment1;
			case 1:
				return fragment2;

			}
			return null;
		}

		@Override
		public int getCount() {

			return 2;
			//return 3;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			Locale l = Locale.getDefault();
			switch (position) {
			case 0:
				return "Player";
			case 1:
				return "Youtube";

			}
			return null;
		}
	}
	
	//timer methods
	public void startTimer(View v)
    {        
    	if(resetFlag)
    		chro.setBase( SystemClock.elapsedRealtime() );
    	
    	else if(pauseFlag)
    		chro.setBase( chro.getBase() + SystemClock.elapsedRealtime() - pausedAt );
    	
    	pauseFlag = false;
    	resetFlag = false;
        chro.start();
    }
    
    public void stopTimer(View v)
    {
    	pauseFlag = true;
    	pausedAt = SystemClock.elapsedRealtime();
        chro.stop();
    }
    
    public void resetTimer(View v)
    {        
    	resetFlag=true;
    	chro.stop();
        chro.setBase(SystemClock.elapsedRealtime());
    }
}
